#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import pygame, sys
import pygame.sprite

pygame.init()
SCREEN = pygame.display.set_mode((400, 300))
pygame.display.set_caption('红蓝大碰撞')

class Block(pygame.sprite.Sprite):
    # 精灵类型🧚‍♂️
    def __init__(self, size, color=(255,0,0), position=(80,0)):
        self.size = size
        self.color = color
        self.position = position

    def draw(self):
        pygame.draw.rect(SCREEN, self.color, self.toRect())

    def toRect(self):
        return pygame.Rect(self.position+self.size)

    def collide(self, other):
        return self.toRect().colliderect(other.toRect())

    def move(self, x):
        self.position = (self.position[0] + x[0],
        self.position[1] + x[1])

blue_rect = Block(size=(100, 20), position=(110, 250), color=(0,0,255))
red_rect = Block(size=(20, 50), position=(85, 0), color=(255,0,0))

goal = False
while True: 
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()
        elif event.type == pygame.KEYDOWN:
            if event.key == pygame.K_a:
                blue_rect.move((-5, 0))
            elif event.key == pygame.K_d:
                blue_rect.move((5, 0))

    red_rect.move((0,5))

    if blue_rect.collide(red_rect) or goal is True:
        blue_rect.color = (255,0,255)
        blue_rect.draw()
        goal = True
    else:
        SCREEN.fill((255, 255, 255))
        red_rect.draw()
        blue_rect.draw()

    pygame.display.update()
    pygame.time.delay(50)
